﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PikachuLevelSelect : MonoBehaviour
{

    //    private Transform gameModePanel;
    private Transform levelTrans;
    private ScrollRect scrollRect;
    private Vector2 originSize;

    private RectTransform nor_content;
    private RectTransform middle_content;
    private RectTransform hard_content;

    public LoadingScene loadingScene;

    // Use this for initialization
    public void Init()
    {
        levelTrans = transform.Find("Level");
        scrollRect = transform.Find("Scroll View").GetComponent<ScrollRect>();
        nor_content = scrollRect.viewport.transform.Find("Normal_Content") as RectTransform;
        middle_content = scrollRect.viewport.transform.Find("Middle_Content") as RectTransform;
        hard_content = scrollRect.viewport.transform.Find("Hard_Content") as RectTransform;

        //初始化关卡信息
        InstanLevel();
    }

    public void showLevelPanel(int mode)
    {
		//InsAdEvaluate.instance.OnlyShowInterstitialAd();
        Debug.Log("打开关卡。。。");
        gameObject.SetActive(true);

        if (mode == 1)
            scrollRect.content = nor_content;
        else if (mode == 2)
            scrollRect.content = middle_content;
        else
            scrollRect.content = hard_content;

        nor_content.gameObject.SetActive(mode == 1);
        middle_content.gameObject.SetActive(mode == 2);
        hard_content.gameObject.SetActive(mode == 3);


        //loadingScene.loadLevelGamplayNormal();
    }

    public void hideLevelPanel()
    {
		//InsAdEvaluate.instance.OnlyShowInterstitialAd();
        gameObject.SetActive(false);
    }

    //void LoadLevel(int mode,int level)
    //{
    //    loadingScene.loadLevel(mode, level);
    //    Debug.Log(mode);
    //    Debug.Log(level);
    //}

    private void InstanLevel()
    {
        for (int i = 0; i < 3; i++)
        {
            int count = 0;
            int temp_mode = i;
            RectTransform parent;
            if (i == 0)
            {
                count = GameConfig.num_level;
                parent = nor_content;
            }
            else if (i == 1)
            {
                parent = middle_content;
                count = GameConfig.num_level_hard1;
            }
            else
            {
                parent = hard_content;
                count = GameConfig.num_level_hard2;
            }

            for (int j = 1; j <= count; j++)
            {
                int temp_level = j;
                Transform level = Instantiate(levelTrans, parent);
                level.gameObject.SetActive(true);
                bool islock = OPlayerPrefs.GetBool("Pikachu_Level" + i + "#" + j);
                if (j == 1 ) {
                    islock = true;
                }
                
                if(islock)
                    level.GetComponent<Button>().onClick.AddListener(()=>{
                        loadingScene.loadLevel(temp_mode, temp_level);
                    });
                Debug.Log("islock : " + islock);
                level.Find("unlock").gameObject.SetActive(islock);
                level.Find("locked").gameObject.SetActive(!islock);
                level.Find("unlock/Text").GetComponent<Text>().text = j.ToString();
                //scrollRect.content.transform
            }
            originSize = parent.GetComponent<RectTransform>().sizeDelta;

            int itemImageCount = parent.childCount;
            float ScrollRectY = Mathf.Ceil(itemImageCount / 5) * ((levelTrans.GetComponent<RectTransform>().sizeDelta.y)
                + parent.GetComponent<GridLayoutGroup>().spacing.y) + parent.GetComponent<GridLayoutGroup>().padding.top;
            Debug.Log("ScrollRectY : " + ScrollRectY);
            parent.GetComponent<RectTransform>().sizeDelta = new Vector2(originSize.x, ScrollRectY);
        }

    }

}
